package com.nbjtjc.safe.api.edu;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.annotation.SysLog;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.edu.EduDailyInfoMapper;
import com.nbjtjc.safe.mapper.sys.SysDeptMapper;
import com.nbjtjc.safe.model.edu.EduDailyInfo;
import com.nbjtjc.safe.model.edu.EduDailyParticipant;
import com.nbjtjc.safe.model.edu.EduDailyInfo;
import com.nbjtjc.safe.model.edu.EduRequirementRespondent;
import com.nbjtjc.safe.model.sys.SysDept;
import com.nbjtjc.safe.model.sys.SysNotice;
import com.nbjtjc.safe.service.edu.EduDailyInfoService;
import com.nbjtjc.safe.service.sys.SysDeptService;
import com.nbjtjc.safe.service.sys.SysNoticeService;
import com.nbjtjc.safe.util.MyShiroUtils;

@RestController
@CrossOrigin("*")
@RequestMapping("/eduDailyInfo")
public class EduDailyInfoController {
	@Autowired
	private EduDailyInfoService eduDailyInfoService;
	@Autowired
	private EduDailyInfoMapper eduDailyInfoMapper;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysDeptMapper sysDeptMapper;
	@Autowired
	private SysNoticeService sysNoticeService;


	  @RequestMapping(value = "/addOrUpdate",method = {RequestMethod.POST})
	  @SysLog("添加或更新日常培训")
	    public Result add(@RequestBody EduDailyInfo eduDailyInfo){
	        Result result=new Result();
	        try {
	        	int ifAdd=0;
	        	if(ObjectUtil.isNull(eduDailyInfo)){
	        		ifAdd=1;
	        	}
	        	
	            result=eduDailyInfoService.add(eduDailyInfo);
	            
	            //通知参与人员
	            if(result.isSuccess()==true
	            		&&ObjectUtil.isNotNull((EduDailyInfo) result.getData())
	            		&&CollectionUtil.isNotEmpty(((EduDailyInfo) result.getData()).getEduDailyParticipants())){
	            	SysNotice sysNotice = new SysNotice();
	            	
	 	            List<Integer> userIds=new ArrayList();

	 	            for(EduDailyParticipant eduDailyParticipant:((EduDailyInfo) result.getData()).getEduDailyParticipants()){
	 	            	if(ObjectUtil.isNotNull(eduDailyParticipant)&&ObjectUtil.isNotNull(eduDailyParticipant.getUserId())){
		 	            	userIds.add(eduDailyParticipant.getUserId());
	 	            	}
	 	            }
	 	            
	            	if(ifAdd==1){
	            		sysNotice.setContent("活动已发布，请到日常培训模页面参与活动。");
	            	}else if(ObjectUtil.isNotNull(((EduDailyInfo) result.getData()).getStatus())&&((EduDailyInfo) result.getData()).getStatus()==3){
	            		sysNotice.setContent("日常培训已结束，请及时评价");
	            		userIds.clear();
		 	            for(EduDailyParticipant eduDailyParticipant:((EduDailyInfo) result.getData()).getEduDailyParticipants()){
		 	            	if(ObjectUtil.isNotNull(eduDailyParticipant)&&ObjectUtil.isNotNull(eduDailyParticipant.getUserId())
		 	            			&&ObjectUtil.isNotNull(eduDailyParticipant.getParticipate())
		 	            			&&eduDailyParticipant.getParticipate()==true){
			 	            	userIds.add(eduDailyParticipant.getUserId());
		 	            	}
		 	            }
	            	}else if(ObjectUtil.isNotNull(((EduDailyInfo) result.getData()).getStatus())&&((EduDailyInfo) result.getData()).getStatus()==1){
	            		sysNotice.setContent("日常培训信息更改，请注意查看");
	            	}else {
	        	        return result;
	            	}
	 	            sysNotice.setCompanyId(MyShiroUtils.getCompanyId());
	 	            sysNotice.setWriteUserId(MyShiroUtils.getSessionUser().getUserId());
	 	            sysNotice.setDeptId(MyShiroUtils.getDeptId());
	 	            sysNotice.setCreateTime(new Date());
	 	            sysNotice.setIdType(9);
	 	            sysNotice.setRelateId(eduDailyInfo.getId());
	 	            sysNotice.setNoticeType(0);
	 	            sysNotice.setTitle("日常培训通知");

	 	            boolean f = sysNoticeService.addNoticeList(sysNotice, userIds);
	 	            if(!f){
	 	            	result.setFalse("通知发送失败");
	 	            }
	            }
	            
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }

	  
	  @RequestMapping(value = "/delete",method = {RequestMethod.POST})
	  @SysLog("删除日常培训")
	    public Result delete(@RequestBody EduDailyInfo eduDailyInfo) {
	        Result result=new Result();
	        try {
	            result=eduDailyInfoService.delete(eduDailyInfo.getId());
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	  //普通员工 的查询，只能查看本公司的需求调查
	  @RequestMapping(value = "/find", method = {RequestMethod.POST})
	  public Result find(@RequestBody EduDailyInfo eduDailyInfo) {
	        Result result = new Result();

	        try {
	        	if(ObjectUtil.isNull(eduDailyInfo.getCompanyId())||eduDailyInfo.getCompanyId()<=0){
	  	  	      eduDailyInfo.setCompanyId(MyShiroUtils.getCompanyId());
	        	}

	        		result = eduDailyInfoService.find(eduDailyInfo);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
//	  //管理人员的查询，非本公司的需求调查也可查看
//	  @RequestMapping(value = "/managerFind", method = {RequestMethod.POST})
//	  public Result managerFind(@RequestBody EduDailyInfo eduDailyInfo) {
//	        Result result = new Result();
//
//	        try {
//	        		result = eduDailyInfoService.find(eduDailyInfo);
//	        } catch (Exception e) {
//	            e.printStackTrace();
//	            result.setFalse(e.getMessage());
//	        }
//	        return result;
//	    }
	  
	  //管理人员的查询，非本公司的需求调查也可查看
	  @RequestMapping(value = "/managerFind", method = {RequestMethod.POST})
	  public Result managerFind(@RequestBody EduDailyInfo eduDailyInfo) {
	        Result result = new Result();

	        try {	        

	        		List<Integer> sysDeptIds = new ArrayList();
		        	SysDept sysDept = new SysDept();
		        	sysDept = sysDeptMapper.selectByPrimaryKey(MyShiroUtils.getCompanyId());
		        	sysDeptIds = sysDeptService.getAllParentCompanyId(sysDept, sysDeptIds);
		        	
	        		PageHelper.startPage(eduDailyInfo.getPageCurrent(), eduDailyInfo.getPageSize());

	        		List<EduDailyInfo> eduDailyInfos = eduDailyInfoMapper.managerFindDetail(eduDailyInfo,sysDeptIds);
	        		result.setData(new PageInfo<>(eduDailyInfos));
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	  @RequestMapping(value = "/show", method = {RequestMethod.POST})
	  public Result show(@RequestBody EduDailyInfo eduDailyInfo) {
	        Result result = new Result();

	        try {
	        	if(ObjectUtil.isNull(eduDailyInfo)||ObjectUtil.isNull(eduDailyInfo.getId())){
	        		result.setFalse("传入ID为空");
	        		return result;
	        	}
	            result = eduDailyInfoService.show(eduDailyInfo.getId());
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	  
	  
	  @RequestMapping(value = "/getUserTrainningHours", method = {RequestMethod.POST})
	  public Result getUserTrainningHours() {
	        Result result = new Result();

	        try {
	            int trainingHours = eduDailyInfoMapper.getUserTrainningHours(MyShiroUtils.getSessionUserId());
	            result.setData(trainingHours);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
}
